package com.vimi8.chick.plugins.widgets.video

import android.content.Context
import android.util.AttributeSet
import android.view.Surface
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer
import com.vimi8.chick.plugins.R
import kotlinx.android.synthetic.main.monitor_video.view.*

class MonitorVideo(context: Context?, attrs: AttributeSet?) : StandardGSYVideoPlayer(context, attrs) {
    override fun init(context: Context?) {
        super.init(context)
        initView()
    }

    override fun getLayoutId(): Int = R.layout.monitor_video

    private fun initView() {
        changeRotate.setOnClickListener {
            if (mHadPlay) {
                val rotate = mRotate.toFloat()
                if (mTextureView.rotation - rotate == 90f) {
                    mTextureView.rotation = rotate
                    changeRotate.setImageResource(R.drawable.ic_rotate_right_white_24dp)
                } else {
                    mTextureView.rotation = mTextureView.rotation + 90f
                    changeRotate.setImageResource(R.drawable.ic_rotate_left_white_24dp)
                }
                mTextureView.requestLayout()
            }
        }
    }

    override fun onSurfaceAvailable(surface: Surface?) {
        super.onSurfaceAvailable(surface)
        resolveRotateUI()
    }

    private fun resolveRotateUI() {
        if (!mHadPlay) {
            return
        }
        mTextureView.rotation = mRotate.toFloat()
        mTextureView.requestLayout()
    }
}
